package cn.peach.pet.controller;

import cn.peach.basic.util.AjaxResult;
import cn.peach.basic.util.PageList;
import cn.peach.pet.domain.Pet;
import cn.peach.pet.query.PetQuery;
import cn.peach.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    /**
     * 加载上线的宠物详情
     * @param id
     * @return
     */
    @GetMapping("/selectOnLineById/{id}")
    public AjaxResult selectOnlineById(@PathVariable("id") Long id){
        try {
            return AjaxResult.me().subResultObj(petService.selectOnlineById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("详情繁忙..");
        }
    }

    /**
     * 加载上线宠物列表
     * @param petQuery
     * @return
     */
    @PostMapping("/pageListForOnsale")
    public AjaxResult pageListForOnsale(@RequestBody PetQuery petQuery){
        try {
            petQuery.setState(Pet.STATE_ONLINE);
            PageList pageList = petService.queryList(petQuery);
            return AjaxResult.me().subResultObj(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("上线宠物系统繁忙");
        }
    }

    /**
     * 加载宠物数据列表
     * @param petQuery
     * @return
     */
    @PostMapping("/pagelist")
    public AjaxResult pagelist(PetQuery petQuery){
        try {
            PageList pageList = petService.queryList(petQuery);
            return AjaxResult.me().subResultObj(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("无法加载数据列表");
        }
    }

    /**
     * 宠物上架
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            petService.onsale(ids,Pet.STATE_ONLINE);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("上架系统故障....");
        }
    }

    /**
     * 宠物下架
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offSale(@RequestBody List<Long> ids){
        try {
            petService.offSale(ids,Pet.STATE_OFFLINE);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("下架系统故障....");
        }
    }
}
